package com.jtjrenren.android.taxi.passenger.core.order.place;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;
import com.jtjrenren.android.taxi.passenger.Constants;
import com.jtjrenren.android.taxi.passenger.bean.AddressInfo;
import com.jtjrenren.android.taxi.passenger.db.dao.AddressDao;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.utils.ModuleUtils;
import com.library.mvp.presenter.BasePresenter;

import com.library.utils.LogUtil;
import com.library.utils.StringUtils;

import java.util.List;

/**
 * Created by wdl on 2016/9/20.
 */

public class PlaceChoosePresenterImpl extends BasePresenter<PlaceChooseView, PlaceChooseInteractor>
        implements PlaceChoosePresenter {

    private GeoCoder geoCoder;
    private SuggestionSearch suggestionSearch;
    private AddressInfo parmAddr;
    private AddressInfo compayAddr, homeAddr, otherAddr;

    public PlaceChoosePresenterImpl(PlaceChooseView view) {
        super(view);
    }

    @Override
    public void init() {
        initGecoder();
        initSugest();
    }

    @Override
    public void destory() {
        if (suggestionSearch != null) {
            suggestionSearch.destroy();
            suggestionSearch = null;
        }
        if (geoCoder != null) {
            geoCoder.destroy();
            geoCoder = null;
        }
    }

    @Override
    public void initAddr(AddressInfo info) {
        if (info==null||(info!=null&&StringUtils.isEmpty(info.getName()))){
            getHistory();
            return;
        }
        parmAddr = info;
        view.refreshKey(info.getName());
        if (!StringUtils.isEmpty(info.getLati()) && !StringUtils.isEmpty(info.getLongi())) {
            LatLng latLng = new LatLng(Double.parseDouble(info.getLati()), Double.parseDouble(info.getLongi()));
            reverseGeoCode(latLng);
        }
    }

    @Override
    public void initUsedShow(boolean isShow) {
        if (isShow) {
            view.showUsedView();
            refreshUsed();
        }
    }

    @Override
    public void initGecoder() {
        geoCoder = GeoCoder.newInstance();
        geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
            @Override
            public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

            }

            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
                view.refreshAddrList(ModuleUtils.convertPoiListToAddresList(reverseGeoCodeResult.getPoiList()));
            }
        });
    }

    @Override
    public void initSugest() {
        suggestionSearch = SuggestionSearch.newInstance();
        suggestionSearch.setOnGetSuggestionResultListener(new OnGetSuggestionResultListener() {
            @Override
            public void onGetSuggestionResult(SuggestionResult suggestionResult) {
                view.refreshAddrList(ModuleUtils.convertSugListToAddresList(suggestionResult.getAllSuggestions()));
            }
        });
    }

    @Override
    public void reverseGeoCode(LatLng latLng) {
        if (geoCoder != null)
            geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(latLng));
    }

    @Override
    public void sugest(String key) {
        if (parmAddr != null && key.equals(parmAddr.getAddr()))
            return;
        if (suggestionSearch != null && AppManger.getInstance().currentLocation != null && !StringUtils.isEmpty(key)) {
            String city = AppManger.getInstance().currentLocation.getCity();
            SuggestionSearchOption option = new SuggestionSearchOption().keyword(key).city(city);
            suggestionSearch.requestSuggestion(option);
        }
    }

    @Override
    public void getHistory() {
        LogUtil.log("place","获取历史信息:");
        List<AddressInfo> history = interactor.getHistory("");
        if (history != null)
            view.refreshAddrList(history);
    }

    @Override
    public void refreshUsed() {
        AddressDao dao = new AddressDao(AppManger.getInstance().getContext());
        List<AddressInfo> list = dao.getAddrListByType(Constants.CONFIG_ADDR_TYPE_COMPANY);
        if (list.size() > 0) {
            compayAddr = list.get(0);
            LogUtil.log("order","company="+compayAddr.toString());
            list.clear();
            view.refreshCompany(compayAddr.getName());
        }
        list = dao.getAddrListByType(Constants.CONFIG_ADDR_TYPE_HOME);
        if (list.size() > 0) {
            homeAddr = list.get(0);
            LogUtil.log("order","homeAddr="+homeAddr.toString());
            list.clear();
            view.refreshHome(homeAddr.getName());
        }
        list = dao.getAddrListByType(Constants.CONFIG_ADDR_TYPE_OTHER);
        if (list.size() > 0) {
            otherAddr = list.get(0);
            LogUtil.log("order","otherAddr="+otherAddr.toString());
            list.clear();
            view.refreshOther(otherAddr.getName());
        }
        list = null;
        dao = null;
    }

    @Override
    public void refreshAddrSave(int type, AddressInfo info) {
        info.setType(type);
        AddressDao dao = new AddressDao(AppManger.getInstance().getContext());
        dao.addNewAddress(info);
        dao = null;

        LogUtil.log("order","位置信息："+info.toString());

        if (type == Constants.CONFIG_ADDR_TYPE_OTHER) {
            otherAddr=info;
            view.refreshOther(info.getName());
        } else if (type == Constants.CONFIG_ADDR_TYPE_HOME) {
            homeAddr=info;
            view.refreshHome(info.getName());
        } else if (type == Constants.CONFIG_ADDR_TYPE_COMPANY) {
            compayAddr=info;
            view.refreshCompany(info.getName());
        }
    }

    @Override
    public void selectedAddr(int type, AddressInfo info) {
        if (type == Constants.CONFIG_ADDR_TYPE_HOME) {
            if (homeAddr != null)
                info = homeAddr;
            else {
                view.chooseHome();
                return;
            }
        }else if (type==Constants.CONFIG_ADDR_TYPE_COMPANY){
            if (compayAddr != null)
                info = compayAddr;
            else {
                view.chooseCompany();
                return;
            }
        }else if (type==Constants.CONFIG_ADDR_TYPE_OTHER){
            if (otherAddr != null)
                info = otherAddr;
            else {
                view.chooseOther();
                return;
            }
        }

        if (info != null) {
            AddressDao dao = new AddressDao(AppManger.getInstance().getContext());
            dao.addNewAddress(info);
            dao = null;

            view.onFinish(info);
        }
    }

    @Override
    public PlaceChooseInteractor createInteractor() {
        return new PlaceChooseInteractorImpl();
    }
}
